var page = sm("do_Page");
var audio = sm("do_Audio");//音频播放与录制
var deviceUtil = require("helper/device");
var nf = sm("do_Notification");
var sendBtn = ui("sendBtn");
var imgAddBtn = ui("btnImgAdd");//加号-按钮
var layout2Btn = ui("layout2Btn");//键盘或麦克风
var voiceBtn = ui("voiceBtn");//麦克风-按钮
var keyBordBtn = ui("keyBordBtn");//键盘-按钮
var btnSayVoice = ui("btnSayVoice");//按住说话-按钮
var textContent = ui("textContent");//文本输入框
var textContentBg = ui("textContentBg");
textContent.setFocus(false);

var audioType = "mp3";
if(deviceUtil.getOsType()=="2"){//IOS自动登陆
	audioType = "amr";//IOS录制mp3会变成【小黄人版】声音
}
var isNetWorkOK = true;//当前网络是否OK
var network = sm("do_Network");
network.on("changed", function(nstatus) {
	if(!nstatus || nstatus==null || "none"===nstatus.toLowerCase()){
		isNetWorkOK = false;
		nf.toast("当前没有网络!",100,250);
	}else{
		isNetWorkOK = true;
	}
});

//输入框--得到焦点
textContent.on("focusIn", function(data, e) {
	page.fire("focusIn");
})
//文字改变
textContent.on("textChanged", function(data, e) {
	//deviceone.print("textChanged"+textContent.text.length);
	if(textContent.text.length>0){
		sendBtn.visible=true;
		imgAddBtn.visible=false;
	}else{
		sendBtn.visible=false;
		imgAddBtn.visible=true;
	}
})

//发送文字消息
sendBtn.on("touch", function(data, e) {
	if(!isNetWorkOK){
		nf.toast("当前没有网络!",100,250);
		return;
	}
	page.fire("im_to_send_text", textContent.text);
	textContent.text = "";
})

//选择图片
imgAddBtn.on("touch", function(data, e) {
	if(btnSayVoice.text!="按住说话"){//正在录音
		return;
	}
	if(!isNetWorkOK){
		nf.toast("当前没有网络!",100,250);
		return;
	}
	page.fire("im_to_select_image");
});

//点击 键盘或麦克风
layout2Btn.on("touch", function(data, e) {
	page.fire("hide_select_image_div");
	if(btnSayVoice.text!="按住说话"){//正在录音
		return;
	}
	if(keyBordBtn.visible){
		keyBordBtn.hide();
		voiceBtn.show();
		btnSayVoice.hide();
		textContent.show();
		textContentBg.show();
	}else if(voiceBtn.visible){
		voiceBtn.hide();
		keyBordBtn.show();
		btnSayVoice.show();
		textContent.hide();
		textContentBg.hide();
	}
});


var need2SendAudio = false;//是否要发送语音消息（用来控制：如果手向上滑动，就取消发送语音）
var audioFilename;//录音暂存的文件名
//订阅音频录制的进度
var audioLen =  0;//录音时间（秒）
var maxAudioLen = 59000;//最长录音长度限制（59秒）
var surplusMins = 0;//剩余录音时长【秒】
audio.on("recordProgress", function(data) {
    //返回录制的时间长度，单位是毫秒，不能超过60000毫秒【一分钟】
	audioLen = data;//毫秒
	surplusMins = Math.round((maxAudioLen-audioLen)/1000);//倒数秒数
	if(surplusMins>10){
		//deviceone.print("录音时长，当前已录:" + audioLen + " 毫秒");
	}else if(surplusMins<10 && surplusMins>=0){
		//当录音接近时限小于10秒时，开始显示倒数提示		
		page.fire("audioBox",false);//隐藏【正在录音】
		page.fire("audioTimeLess",{isShow:true,tip:"录音过长,即将结束.",lastMin:surplusMins});//显示【倒数】
	}else if(surplusMins < 0){
		//当达到时限时，强制停止并发送这语音
		forece2stopRecord();
	}
});

//订阅音频录制结束的事件
var lastSendAudioFilename = "";//上一次发送语音path【防止重复发送同一个语音文件】
var audioLenInt = 0;//秒
audio.on("recordFinished", function(data) {
	if(audioFilename == "undefined" || audioFilename == undefined){
		deviceone.print("语音文件无效!");
		return;
	}
	//deviceone.print("录音结束，保存到:" + audioFilename);
    if(audioLen>=800){//如果录音时长800毫秒以上就发送
    	if(need2SendAudio){
    		audioLenInt = Math.round(audioLen/1000);//四舍五入（秒）
    		if(lastSendAudioFilename == audioFilename){
    			deviceone.print(audioFilename+" "+audioLen+"毫秒","已发送过语音");
    		}else{
    			deviceone.print(audioFilename+" "+audioLen+"毫秒","发送语音");
        		page.fire("im_to_send_audio",{filePath:audioFilename,fileLen:audioLenInt});//发送语音
        		lastSendAudioFilename = audioFilename;
    		}
    	}else{
    		nf.toast("您已取消发送这段语音.");
    	}
	}
});

//按下并在按钮范围抬起，触发该事件（如果离开按钮范围抬起就不会触发该事件,利用这点可实现向上滑动来取消发送语音）
//这里用来“发送语音”


btnSayVoice.on("touch", function(data, e) {
	//deviceone.print("触发 touch");
	need2SendAudio = true;
});

//按钮范围内按下即可触发touchDown
//按住说话-按下
btnSayVoice.on("touchDown", function(data, e) {
	page.fire("hide_select_image_div");
	if(!isNetWorkOK){
		nf.toast("当前没有网络!",100,250);
		return;
	}
	//deviceone.print("触发 touchDown");
	need2SendAudio = false;//这里每次强制设为不发送语音，当【触发 touch】时改为“要发送”
	audioLen=0;//重置录音时间
	btnSayVoice.text = "正在录音...";
	btnSayVoice.fontColor = "FF0000FF";
    page.fire("audioBox",true);
	// 录音保存到一个data根目录,选择录音输出的格式，支持mp3、amr、aac格式,	选择录音输出的质量，支持high、normal、low
    audio.startRecord({path:"data://audio/record", type: audioType, quality:"low"});// amr压缩率高，适合网络传输
});

//一旦按下，手指离开即触发touchUp，不论是否在按钮范围内
//按住说话-放开
btnSayVoice.on("touchUp", function(data, e) {
	//deviceone.print("触发  touchUp");
	btnSayVoice.text="按住说话";
	btnSayVoice.fontColor = "000000FF";
	page.fire("audioBox",false);
	audioFilename = audio.stopRecord();
	//deviceone.print(audioFilename+" 时间："+audioLen+"毫秒","audioFilename");
	page.fire("audioTimeLess",{isShow:false,tip:"",lastMin:""});//隐藏【倒数】
});

//超过1分钟，强制停止录制，并发送（同时提醒录音过程，不能再录）
function forece2stopRecord(){
	need2SendAudio = true;
	btnSayVoice.text="按住说话";
	btnSayVoice.fontColor = "000000FF";
	page.fire("audioBox",false);
	page.fire("audioTimeLess",{isShow:false,tip:"",lastMin:""});//隐藏【倒数】
	audioFilename = audio.stopRecord();
	//deviceone.print(audioFilename+" 时间："+audioLen+"毫秒","强制停止录音audioFilename");
}
